<?php
//vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldcolumn=1 foldmethod=marker:
/**
 * Project: ltebbs
 * File:    install.php
 *
 * Install ltebbs in your server
 *
 * after install the ltebbs in your server, please remove this script
 *
 * PHP Version 5
 *
 * @package:   NULL
 * @author:    Mike.G   Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: http://www.5anet.com
 * @version:   $Id: index.php,v 1.2 2006-09-24 14:38:08 ghw Exp $
 * @date:      $Date: 2006-09-24 14:38:08 $
 */

//we don't limited the run time limition.
set_time_limit(0);
session_start();


//get the step parameter.

$step = $_SERVER['REQUEST_METHOD'] == 'GET' ? $_GET['step']:$_POST['step'];


if ( !$step ) {
    header('Content-type:text/html;charset=UTF-8');
    showDefaultPage();
    exit(0);
} else if ( $step == 1 ) {
    header("Content-type:text/html;charset=UTF-8");
    showFileConfig();
    exit(0);
} else if ( $step == 2 ) {
    header("Content-type:text/html;charset=UTF-8");
    checkPath();
    exit(0);
} else if ( $step == 3 ) {
    header("Content-type:text/html;charset=UTF-8");
    showDatabase();
    exit(0);
} else if ( $step == 4 ) {
    header("Content-type:text/html;charset=UTF-8");
    checkDatabase();
    exit(0);
} else if ( $step == 5 ) {
    header("Content-type:text/html;charset=UTF-8");
    complete();
    exit(0);
}



/**
 * define the install function 
 */

function showDefaultPage() {/*{{{*/

    echo <<<DEFAULT
<html>
    <head>
    <title>Install Ltebbs into your server</title>
    </head>
    <body>
    <h1>LTEBBS简易安装脚本</h1>
    <table width="65%" border=1 cellpadding=3 cellspacing=3>
    <tr align="left" valign="middle">
    <td>
        <b>作者:<b>
    </td>
    <td >
        黄叶/Mike.G
    </td>
    </tr>

    <tr align="left" valign="middle">
    <td>
    <b>邮件:</b>
    </td>
    <td> 
    <a href="mailto:hylinux@gmail.com">hylinux@gmail.com</a><br>
    </td>
    </tr>

    <tr align="left" valign="middle">
    <td>
    <b>开发网站:</b>
    </td>
    <td>
    <a href="http://www.5anet.com" target="_blank">www.5anet.com</a>
    </td>
    </tr>

    <tr align="left" valign="middle">
    <td>
    <b>Subversion:</b>
    </td>
    <td>
    http://ltebbs.googlecode.com/svn/trunk
    </td>
    </tr>

 
    <tr align="left" valign="middle">
    <td>
    <b>功能需求:</b>
    </td>
    <td>
    <a href="http://www.5anet.com/index.php?module=bbs&action=viewlayout&id=34" target="_blank">请点击这里进入</a>
    </td>
    </tr>
 
    <tr align="left" valign="middle">
    <td>
    <b>帮助区:</b>
    </td>
    <td>
    <a href="http://www.5anet.com/index.php?module=bbs&action=viewlayout&id=35" target="_blank">请点击这里进入</a>
    </td>
    </tr>
 
    <tr align="left" valign="middle">
    <td>
    <b>bug跟踪区:</b>
    </td>
    <td>
    <a href="http://www.5anet.com/index.php?module=bbs&action=viewlayout&id=36" target="_blank">点击这里进入</a>
    </td>
    </tr>
    
    <tr align="left" valign="top">
    <td>
    <b>许可证:</b>
    </td>
    <td>
    http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    </td>
    </tr>

    </table><br>

    <input type="button" value="同意并安装" OnClick="document.location.href='install.php?step=1'">
    </body>
    </html>
DEFAULT;
}/*}}}*/

function showFileConfig() {/*{{{*/
    $temp = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    $temp_array = preg_split("/\//", $temp);


    $url1 = 'http://';
    for($i=0; $i<count($temp_array)-1; $i++ ) {
       $url1 .= $temp_array[$i]."/"; 
    }

    $url = $_SESSION['url']?$_SESSION['url']:$url1;
    $path = $_SESSION['path']?$_SESSION['path']:dirname(__FILE__)."/";
    $cache = $_SESSION['cache']?$_SESSION['cache']:$path.'cache/';

    echo <<<SHOWFILECONFIG
<html>
<head>
<title>Directory and URL configuration</title>
</head>
<body>
<h1>LTEBBS简易安装脚本 - 设定访问URL和存储路径</h1>
<br>
<form action="install.php" method="post">
<font color="red">注意：所有路径均应该以"/"结尾</font><br>
<table width="55%" left="left" cellpadding=3 cellspacing=3 border="1">

<tr align="left">
<td>
访问URL:
</td>
<td>
<input type="text" name="url" value="$url"/ size=35>
</td>
</tr>

<tr align="left">
<td>
代码安装路径:
</td>
<td>
<input type="text" name="path" value="$path"/ size=35>
</td>
</tr>

<tr align="left">
<td>
缓存目录:
</td>
<td>
<input type="text" name="cache" value="$cache"/ size=35>
</td>
</tr>
</table>
<input type="hidden" name="step" value=2>
<input type="button" value="上一步" OnClick="document.location.href='install.php'">&nbsp;&nbsp;
<input type="submit" value="下一步">

</form>

</body>
</html>


SHOWFILECONFIG;
}
/*}}}*/


function checkPath() {/*{{{*/
    $url = $_POST['url'];
    $cache = $_POST['cache'];
    $path = $_POST['path'];

    $_SESSION['url'] = $url;
    $_SESSION['cache'] = $cache;
    $_SESSION['path'] = $path;
    ob_end_flush();
    $have_failed = 0;
    $failed_error = '';

    echo <<<CHECKPATH
<html>
<head>
<title>Check Install Path</title>
</head>
<body>
<h1>LTEBBS简易安装脚本 - 检测安装路径</h1>
<hr width="45%" align="left">

CHECKPATH;

    //检测访问的URL
    $content = file_get_contents($url);

    if ( strlen($content) > 0 ) {
        if ( preg_match("/\/$/", $url) ) {
            echo "1. 检测访问URL .................................. OK<br>";
        } else {
            echo "1. 检测访问URL .................................. <font color=red>失败<br>";
            $have_failed = 1;
            $failed_error = "<font color=red>访问URL应该以\"/\"结尾</font><br>";
        }
    } else {
        echo "1. 检测访问URL .................................. <font color=red>失败<br>";
        $have_failed = 1;
        $failed_error = "<font color=red>访问URL没有返回</font><br>";
    }

    //检测实际安装的路径

    if ( file_exists($path) ) {
        if( is_dir($path) ) {
            echo "2. 检测安装路径 .................................. OK<br>";
        } else {
            echo "2. 检测安装路径 .................................. <font color=red>失败</font><br>";
            $have_failed = 1;
            $failed_error .= "<font color=red>安装路径不是一个目录</font><br>";
        }
    } else {
        echo "2. 检测安装路径 .................................. <font color=red>失败</font><br>";
        $have_failed = 1;
        $failed_error .= "<font color=red>安装路径不存在</font><br>";
    }

    //检测cache的目录
    if ( file_exists($cache) ) {
        if ( is_dir($cache) ) {
            $my_file_perms = substr(sprintf("%o", fileperms($cache)), -3);
            if ( $my_file_perms == 777 || is_writable($cache) ) {
                echo "3. 检测缓存目录 .................................. OK<br>";
            } else {
                echo "3. 检测缓存目录 .................................. <font color=red>失败</font><br>";
                $have_failed = 1;
                $failed_error .= "<font color=red>缓存目录的权限应该是777 或者是web服务器可写</font><br>";
            }
        } else {
            echo "3. 检测缓存目录 .................................. <font color=red>失败</font><br>";
            $have_failed = 1;
            $failed_error .= "<font color=red>给出的缓存目录不是一个目录</font><br>";
        }
    } else {
        echo "3. 检测缓存目录 .................................. <font color=red>失败</font><br>";
        $have_failed = 1;
        $failed_error .= "<font color=red>缓存目录不存在</font><br>";
    }

    //检测upload目录
    if ( file_exists($path.'upload/') ) {
        if ( is_dir($path.'upload/') ) {
            $my_file_perms = substr(sprintf("%o", fileperms($path.'upload/')), -3);
            if ( $my_file_perms == 777 || is_writable($path.'upload') ) {
                echo "4. 检测上传目录 .................................. OK<br>";
            } else {
                echo "4. 检测上传目录 .................................. <font color=red>失败</font><br>";
                $have_failed = 1;
                $failed_error .= "<font color=red>上传目录".$path."upload/的权限应该是777 或者web服务器可写</font><br>";
            }
        } else {
            echo "4. 检测缓存目录 .................................. <font color=red>失败</font><br>";
            $have_failed = 1;
            $failed_error .= "<font color=red>给出的上传目录".$path."upload/不是一个目录</font><br>";
        }
    } else {
        echo "4. 检测缓存目录 .................................. <font color=red>失败</font><br>";
        $have_failed = 1;
        $failed_error .= "<font color=red>上传目录".$path."upload/'不存在</font><br>";
    }


    if ( $have_failed == 1 ) {
        $_SESSION['have_failed'] = 1;
    } else {
        $_SESSION['have_failed'] = 0;
    }

    if ( $have_failed == 1 ) {
        echo <<<HAVE_FAILED
        <hr width="45%" align="left">
            <font size=4 color=red><b>我们检测到了某些错误</b></font>
            <br>
            <font color=red>$failed_error</font>
            <br>
            <input type="button" value="上一步" OnClick="document.location.href='install.php?step=1'">

            </body>
            </html>
HAVE_FAILED;

    } else {
        echo <<<SUCCESS
    <hr width="45%" align="left">
            <font size=4 color=red><b>检测成功</b></font>
            <br>
            <font color=red>$failed_error</font>
            <br>
            <input type="button" value="上一步" OnClick="document.location.href='install.php?step=1'">
            <input type="button" value="下一步" OnClick="document.location.href='install.php?step=3'">
            </body>
            </html>
SUCCESS;
    }
}
/*}}}*/

function showDatabase() {/*{{{*/

    $db_type = $_SESSION['db_type'];
    $db_host = $_SESSION['db_host'];
    $db_user = $_SESSION['db_user'];
    $db_passwd = $_SESSION['db_passwd'];
    $db_persist = $_SESSION['db_persist'];
    $db_name = $_SESSION['db_name'];

    echo <<<DATABASECHECK
<html>
<head>
<title>Check Database setting</title>
</head>
<body>
<h1>LTEBBS简易安装脚本 - 检测数据库</h1>
<form name="myform" action="install.php" method="post">

<table width="45%" align="left" cellpadding=3 cellspacing=3 border=1>
<tr align="left">
<td width=150>
<b>数据库类型:</b>
</td>
<td>
<select name="db_type">
<option value="mysql" selected>MySQL数据库
</select>
</td>
</tr>

<tr align="left">
<td width=150>
<b>数据库主机:</b>
</td>
<td>
<input type="text" name="db_host" value="$db_host" size=35/>
</td>
</tr>

<tr align="left">
<td width=150>
<b>数据库名:</b>
</td>
<td>
<input type="text" name="db_name" value="$db_name" size=35/>
</td>
</tr>



<tr align="left">
<td width=150>
<b>连接数据库用户:</b>
</td>
<td>
<input type="text" name="db_user" value="$db_user" size=35/>
</td>
</tr>

<tr align="left">
<td width=150>
<b>数据库用户密码:</b>
</td>
<td>
<input type="text" name="db_passwd" value="$db_password" size=35/>
</td>
</tr>

<tr align="left">
<td width=150>
<b>使用持久连接:</b>
</td>
<td>
<input type="checkbox" name="db_persist" value=1 checked/>
</td>
</tr>
</table>
<br>
<br><br><br><br><br><br><br><br><br><br><br>
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td>
<input type="hidden" name="step" value=4>
<input type="button" value="上一步" OnClick="document.location.href='install.php?step=2'"/>
&nbsp;&nbsp;
<input type="submit" value="下一步">
</td>
</tr>
</table>
</form>

</body>
</html>

DATABASECHECK;

}
/*}}}*/

function checkDatabase() {/*{{{*/
    $db_type = $_POST['db_type'];
    $db_host = $_POST['db_host'];
    $db_user = $_POST['db_user'];
    $db_passwd = $_POST['db_passwd'];
    $db_persist = $_POST['db_persist'];
    $db_name = $_POST['db_name'];


    $_SESSION['db_type'] = $db_type;
    $_SESSION['db_host'] = $db_host;
    $_SESSION['db_user'] = $db_user;
    $_SESSION['db_passwd'] = $db_passwd;
    $_SESSION['db_persist'] = $db_persist;
    $_SESSION['db_name'] = $db_name;

    $dbh = @mysql_connect($db_host, $db_user, $db_passwd);

    echo <<<MYNAME
    <html>
<head>
<title>Check Database</title>
</head>
<body>
<h1>LTEBBS简易脚本安装</h1>
MYNAME;

    if ( !$dbh ) {
        echo <<<SHOWERROR
<hr width="45%" align="left">
<font color=red>数据库不能连接上</font><br>
<input type="button" value="上一步" OnClick="javascript:history.back()"/>
SHOWERROR;
        exit;
    } else {
        echo <<<SHOWERROR
<hr width="45%" align="left">
<font color=red>数据库正常</font><br>
SHOWERROR;
    }
echo $db_name, "<br>";
    $sql = "CREATE DATABASE $db_name CHARACTER SET 'utf8'; use $db_name; set names 'utf8';";

    $temp_str = file_get_contents("db/ltebbs.sql");

    $sql .= $temp_str;

    $sql = preg_replace("/\r\n/", "", $sql);
    $sql = preg_replace("/\n/", "", $sql);
    $sql = preg_replace("/\r/", "", $sql);

    $sql_array = preg_split("/;/", $sql);

    for($i=0; $i<count($sql_array)-1; $i++ ) {
        mysql_query($sql_array[$i], $dbh);
    }
    echo mysql_error($dbh);

    if ( !@mysql_errno($dbh) ) {
        echo <<<SHOWERROR
<font color=red>数据库成功建立</font><br>
SHOWERROR;

    } else {
        echo <<<SHOWERROR
<font color=red>数据库建立失败</font>
<font color=red>你可以使用"CREATE DATABASE databasename CHARACTER SET 'UTF8'</font><br>
SHOWERROR;
    }

    echo <<<SHOWHTML
<br>
<input type="button" value="上一步" OnClick="document.location.href='install.php?step=3'">
&nbsp;&nbsp;
<input type="button" value="下一步" OnClick="document.location.href='install.php?step=5'">

</body>
</html>
SHOWHTML;
    return;
}/*}}}*/

function complete() {/*{{{*/
    echo <<<COMPLETE
<html>
<head><title>Complete Install</title></head>
<body>
<h1>LTEBBS简易安装脚本 － 完成安装</h1>
COMPLETE;

    if ( !file_exists("config/config.inc.php") ) {
       echo "<font color=red>config/config.inc.php配置文件不存在!请手工更改Sample文件</font><br>";
       echo "<input type=\"button\" value=\"上一步\" OnClick=\"javascript:history.back()\"/><br>";
       echo "</body>\n";
       echo "</html>\n";
       exit; 
    } else {
        $my_file_perms = substr(sprintf("%o", fileperms('config/config.inc.php')), -3);

        if ( $my_file_perms != 777 && !is_writable('config/config.inc.php') ) {
            echo "<font color=red>config/config.inc.php不可写，如果是Linux文件系统，请更改为777或者是改为web用户所有者</font><br>";
            echo "<input type=\"button\" value=\"上一步\" OnClick=\"javascript:history.back()\"/><br>";
            echo "</body>\n";
           echo "</html>\n";
           exit; 
        }

    }

    $file = file_get_contents("config/config.inc.php");

    $file = preg_replace("/%DB_TYPE%/", $_SESSION['db_type'], $file);
    $file = preg_replace("/%DB_HOST%/", $_SESSION['db_host'], $file);
    $file = preg_replace("/%DB_NAME%/", $_SESSION['db_name'], $file);
    $file = preg_replace("/%DB_USER%/", $_SESSION['db_user'], $file);
    $file = preg_replace("/%DB_PASS%/", $_SESSION['db_passwd'], $file);
    $file = preg_replace("/%DB_PERSIST%/", $_SESSION['db_persist']?$_SESSION['db_persist']:0, $file);
    $file = preg_replace("/%DB_DEBUG%/", 0, $file);

    $file = preg_replace("/%ACCESS_URL%/", $_SESSION['url'], $file);
    $file = preg_replace("/%ACCESS_PATH%/", $_SESSION['path'], $file);
    $file = preg_replace("/%CACHE_PATH%/", $_SESSION['cache'], $file);

    $temp_url = $_SESSION['url'];
    $temp_url = preg_replace("/http:\/\//", "", $temp_url);

    $temp_array = preg_split("/\//", $temp_url);

    $temp_base = '/';
    for($i=1; $i<count($temp_array)-1; $i++ ) {
        $temp_base .= $temp_array[$i];
    }
    
    $fckeditor_base = $temp_base . "/lib/fckeditor/";
    $upload_dir = $temp_base ."/upload/fckeditor/";


    $file = preg_replace("/%FCKEDITOR_BASEPATH%/", $fckeditor_base, $file);
    $file = preg_replace("/%FCKEDITOR_UPLOADDIR%/", $upload_dir, $file);

    $fh = fopen("config/config.inc.php", "w");
    fwrite($fh, $file);
    fclose($fh);

    echo <<<WRITEFILE
<h2><font color=red>配置文件已经生成</font></h2>
你也可以将下面的配置文件内容粘贴成config/config.inc.php文件。<br>
<br>
管理控制台的登录入口是:<a href="${_SESSION['url']}admin/">${_SESSION['url']}admin/</a>, <br>
默认管理员帐号是: admin, 管理密码是：123456
<br>
<br>

WRITEFILE;

    echo highlight_string($file, true), "<br>";

    echo "</body>\n";
    echo "</html>\n";

}/*}}}*/

?>
